package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.result.UserWitness;
import reduce.fz.com.reduce.fragment.FindArtistCategoryFragment;
import reduce.fz.com.reduce.init.RecycleOper;

/**
 * Created by qingshengzheng on 15/9/27.
 */
public class WitnessCardAdapter extends PagerAdapter {
    private LayoutInflater layoutInflater;
    private List<UserWitness> userWitness;
    private WitnessCardListener cardListener;

    /**
     * 构造参数 用户见证列表，内部操作监听
     * @param witness
     * @param listener
     */
    public WitnessCardAdapter(List<UserWitness> witness ,WitnessCardListener listener)
    {
        this.userWitness=witness;
        this.cardListener=listener;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //获取
        View view = layoutInflater.inflate(R.layout.view_witness_card, null);
        MyViewHolder viewHold=new MyViewHolder(view,cardListener);
        viewHold.renderView(userWitness.get(position));

        return view;
    }
    @Override
    public int getCount() {
        return userWitness.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return false;
    }


    public class MyViewHolder  implements View.OnClickListener{
        //用户昵称
        private TextView mTvUserNickName;
        //用户头像
        private ImageView mUserHeader;
        //用户发布见证时间
        private TextView mPubTime;
        //用户主图
        private ImageView mCardMain;
        //用户发布附图
        private RecyclerView mWitnessPics;
        //用户见证描述
        private TextView mWitnessDesc;
        //见证通过按钮
        private ImageView mIvWitnessPass;
        //见证不通过按钮
        private ImageView mIvWitnessNoPass;
        //上下文
        private Context context;
        private WitnessCardListener listener;
        private WitnessPicHorAdapter picAdapter;

        /**
         * 初始化viewHolder
         *
         * @param itemView
         */
        public MyViewHolder(View itemView) {
            initWidget(itemView);
            context=itemView.getContext();
        }

        public MyViewHolder(View itemView,WitnessCardListener listner)
        {
            this(itemView);
            this.listener=listner;
        }

        public void initWidget(View itemView) {
            picAdapter=new WitnessPicHorAdapter(context);
            RecycleOper oper1=new RecycleOper(context,mWitnessPics);
            oper1.setHorDefault();
            mTvUserNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mUserHeader = (ImageView) itemView.findViewById(R.id.user_header);
            mPubTime = (TextView) itemView.findViewById(R.id.pub_time);
            mCardMain = (ImageView) itemView.findViewById(R.id.card_main);
            mWitnessPics = (RecyclerView) itemView.findViewById(R.id.witness_pics);
            mWitnessDesc = (TextView) itemView.findViewById(R.id.witness_desc);
            mIvWitnessPass = (ImageView) itemView.findViewById(R.id.iv_witness_pass);
            mIvWitnessNoPass = (ImageView) itemView.findViewById(R.id.iv_witness_nopass);
        }
        public void renderView(UserWitness wit)
        {
            mTvUserNickName.setText(wit.user.nickname);
            Picasso.with(context).load(wit.user.headurl).into(mUserHeader);
            mPubTime.setText(wit.pubTime.toString());
            Picasso.with(context).load(wit.card.mainImage);
            mWitnessDesc.setText(wit.comments);
        }
        public void initListener()
        {
            mCardMain.setOnClickListener(this);
        }

        /**
         * 点击事件
         * @param v
         */
        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.iv_main_image:
                    listener.onMainImageClick();
                    break;
                case R.id.iv_witness_pass:
                    listener.onWitnessPass();
                    break;
                case R.id.iv_witness_nopass:
                    listener.onWitnessNoPass();
                    break;
                default:
                    break;

            }

        }
    }

    public interface WitnessCardListener
    {
        void onMainImageClick();
        void onWitnessPass();
        void onWitnessNoPass();
        void loadMore();
    }
}
